package com.example.shoppingmallspringboot.controller;

import com.example.shoppingmallspringboot.entity.Cart;
import com.example.shoppingmallspringboot.service.CartService;
import com.example.shoppingmallspringboot.vo.CartVo;
import com.example.shoppingmallspringboot.util.FwResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "CartController", description = "购物车控制器")
@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    @GetMapping("/list/{userId}")
    @Operation(summary = "获取用户购物车", description = "根据用户ID获取购物车")
    public FwResult<List<CartVo>> getUserCart(@PathVariable Integer userId) {
        return FwResult.ok(cartService.getUserCart(userId));
    }

    @PostMapping("/add")
    @Operation(summary = "添加到购物车", description = "添加商品到购物车")
    public FwResult addCart(@RequestBody Cart cart) {
        return cartService.addCart(cart);
    }

    @PostMapping("/delete")
    @Operation(summary = "删除购物车商品", description = "删除购物车中的商品")
    public FwResult deleteCart(@RequestBody List<Cart> cartList) {
        return cartService.deleteCart(cartList);
    }

    @PostMapping ("/changeNum")
    @Operation(summary = "更新商品数量", description = "更新购物车中商品的数量")
    public FwResult<Void> updateQuantity(@RequestBody Cart cart) {
        cartService.updateQuantity(cart);
        return FwResult.ok();
    }
}
